package com.adtech.adms.mainview.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.User;
import com.adtech.adms.system.service.FeatureService;
import com.adtech.core.web.action.BaseAction;

/**
 * @author Dormin 
 * @since 2011-5-23 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class LeftAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String strMenu;
	private FeatureService featureService;

	public String getStrMenu() {
		return strMenu;
	}

	public void setStrMenu(String strMenu) {
		this.strMenu = strMenu;
	}

	/**
	 * 列表初始页面的转发
	 */

	@Override
	public String execute() {
		User user = (User)session.get(SessionKey.SESSION_KEY_USER);
		List<TreeNode<Feature>> nodes = featureService.getTreeNodeListByFeatures(user.getFeaturesTree());
		createMainMenu(nodes);
		return SUCCESS;
	}

	public void setFeatureService(FeatureService featureService) {
		this.featureService = featureService;
	}

	private void createMainMenu(List<TreeNode<Feature>> nodes) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("<ul id='menu'>");
		String servername = ServletActionContext.getRequest().getContextPath();
		for (TreeNode<Feature> treenode : nodes) {
			int nLevel = 1;
			buffer.append("<li class='L1'><a href=\"javascript:c('m"
					+ treenode.getId()
					+ "');\" id=\"m"
					+ treenode.getId()
					+ "\"><span><img src=\""
					+ servername
					+ "/theme/default/images/main/menu/mytable.gif\" align=\"absMiddle\"/> "
					+ treenode.getName() + "</span></a></li>");
			buffer.append("<ul id=\"m" + treenode.getId()
					+ "d\" style=\"display:none;\" class=\"U1\">");
			createChildrenMenu(treenode.getNodes(), buffer, nLevel);
			buffer.append("</ul>");
		}

		buffer.append("</ul>");
		strMenu = buffer.toString();
	}

	private void createChildrenMenu(List<TreeNode<Feature>> nodes, StringBuffer buffer,
			int nLevel) {

		if (nodes == null || nodes.size() == 0) {
			return;
		}

		nLevel++;
		for (TreeNode<Feature> treenode : nodes) {
			if (null != treenode.getNodes() && treenode.getNodes().size() > 0) {
				buffer.append("<li class=\"L21\"><a href=\"javascript:c('p_"
						+ nLevel + "_" + treenode.getId() + "');\" id=\"p_"
						+ nLevel + "_" + treenode.getId() + "\"><span>"
						+ treenode.getName() + "</span></a></li>");
				buffer.append("<ul id=\"p_" + nLevel + "_" + treenode.getId()
						+ "d\" style=\"display:none;\">");
				createChildrenMenu(treenode.getNodes(), buffer, nLevel);
				buffer.append("</ul>");

			} else {
				buffer.append("<li class=\"L" + nLevel
						+ "\"><a href=\"javascript:a('"
						+ treenode.getUrl() + "'," + treenode.getId()
						+ "," + nLevel + ");\" id=\"l_" + nLevel + "_"
						+ treenode.getId() + "\"><span>"
						+ treenode.getName() + "</span></a></li>");

			}

		}
	}

}
